In [1]:
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
import folium
In [3]:
# read in layer and point data
data_df = pd.read_csv('mps_robbery_policeuk12m.csv')

Crime 'Hot Points'¶

In [4]:
#location counts
location_counts = data_df.groupby(['latitude', 'longitude']).size().reset_index(name='count')
In [22]:
def calc_rad(count):
    scaling_factor=2
    return count * scaling_factor
In [23]:
# create map
m = folium.Map(location=[51.5074, -0.1278], zoom_start=14)

for index, row in location_counts.iterrows():
    latitude=row['latitude']
    longitude=row['longitude']
    count=row['count']
    
    radius=calc_rad(count)
    
    folium.Circle(
        location=[latitude, longitude],
        radius=radius,
        color='blue',
        fill=True,
        fill_opacity=0.6,
        pop='# Points: {}'.format(count)
    ).add_to(m)

m
Out[23]:
Make this Notebook Trusted to load map: File -> Trust Notebook

Crime 'Hot Points' and Heatmap¶

In [24]:
from folium.plugins import HeatMap

# create point geo data
geometry = [Point(xy) for xy in zip(data_df['longitude'], data_df['latitude'])]
data_gdf = gpd.GeoDataFrame(data_df, geometry=geometry)
data_gdf = data_gdf.set_crs(4326)
In [27]:
# create map
m = folium.Map(location=[51.5074, -0.1278], zoom_start=14)

# Add heatmap layer
heat_data = [[point.y, point.x] for point in data_gdf.geometry]
HeatMap(
    heat_data,
    radius=15, # radius of each data points influence in pixels
    blur=10, # radius of blur in heatmap pixels
    gradient={0.3: 'white', 0.6: 'yellow', 0.8: 'orange', 0.9: 'red', 1: 'magenta'}, # setting colour gradient
    overlay=True,
    name='Robbery Hotspots'
).add_to(m)

# Add hotpoint layer
hp_group = folium.FeatureGroup(name='Robbery Hot-points')

for index, row in location_counts.iterrows():
    latitude=row['latitude']
    longitude=row['longitude']
    count=row['count']
    
    radius=calc_rad(count)
    
    folium.Circle(
        location=[latitude, longitude],
        radius=radius,
        color='blue',
        fill=True,
        fill_opacity=0.6,
        popup='# Offences: {}'.format(count)
    ).add_to(hp_group)

hp_group.add_to(m)
    
folium.LayerControl().add_to(m)

m
Out[27]:
Make this Notebook Trusted to load map: File -> Trust Notebook